package com.example.zzm.yuejian03.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzm.yuejian03.Adapter.CommentAdapter;
import com.example.zzm.yuejian03.Adapter.PictureAdapter;
import com.example.zzm.yuejian03.Bean.CommentBean;
import com.example.zzm.yuejian03.Bean.PictureBean;
import com.example.zzm.yuejian03.Bean.collectBean;
import com.example.zzm.yuejian03.Listener.CommentListrener;
import com.example.zzm.yuejian03.Listener.TListener;
import com.example.zzm.yuejian03.Model.CommentModel;
import com.example.zzm.yuejian03.Model.PictureModeldetails;
import com.example.zzm.yuejian03.Model.fabucommentModel;
import com.example.zzm.yuejian03.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class PictureActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private EditText editText;
    private TextView textView11,textView25,textView22,textView26,textView24,textView,textView2;
    private ImageView imageView11,imageView;
    private Button button11;
    private int userid;
    private SharedPreferences sp;
    private int resid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);
        //System.out.println("----查看俱乐部详情");
        resid  = getIntent().getIntExtra("show_id",1);//获取传递的资源id
        sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        readSP();//读取sessionid

        initViews();
        initEvent();
        PictureModeldetails ClubModeldetails = new PictureModeldetails();
        ClubModeldetails.getPicturedetailss(getIntent().getIntExtra("show_id",1),listener);
        CommentModel commnetModel = new CommentModel();
        commnetModel.getCommentList(getIntent().getIntExtra("show_id",1),tttlistrener);

    }
    private void readSP() {
        userid = sp.getInt("user_id",1);
    }
    TListener<PictureBean> listener = new TListener<PictureBean>() {
        @Override
        public void onResponse(PictureBean picture) {
            textView22.setText(picture.getOpentime());
            textView24.setText(picture.getNickname());
            textView26.setText(picture.getIntro());
            Picasso.with(PictureActivity.this)
                    .load("http://10.0.2.2/yuejian10/public/static/asstes/images/" + picture.getPic())
                    .placeholder(R.mipmap.ic_launcher)
                    // .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(imageView11);
        }
        @Override
        public void onFail(String msg) {

        }
    };

    CommentListrener tttlistrener = new CommentListrener() {
        @Override
        public void onResponse(List<CommentBean> beanlist) {
            CommentAdapter classAdapter = new CommentAdapter(getApplicationContext(),beanlist);
            recyclerView.setAdapter(classAdapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initViews(){
        textView25 = (TextView)findViewById(R.id.textView25);//mingc
        textView22 = (TextView)findViewById(R.id.textView22);//jieshao
        textView26= (TextView)findViewById(R.id.textView26);//jiaolian
        textView24 = (TextView)findViewById(R.id.textView24);
        imageView11 = (ImageView)findViewById(R.id.imageView11);
        textView11 = (TextView) findViewById(R.id.textView11);//fanhui
        button11 = (Button)findViewById(R.id.button11);
        editText = (EditText)findViewById(R.id.editText);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
    }

    private void initEvent(){
        textView11.setOnClickListener(this);
        button11.setOnClickListener(this);
    }
    TListener<collectBean> ttlistener = new TListener<collectBean>() {
        @Override
        public void onResponse(collectBean baominBean) {
            if (baominBean.getError().equals("0")) {
                CommentModel commnetModel = new CommentModel();
                commnetModel.getCommentList(getIntent().getIntExtra("show_id", 1), tttlistrener);
                editText.setText("");
            }
        }
        @Override
        public void onFail(String msg) {

        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.textView11:
                finish();
                break;
            case R.id.button11:
            fabucommentModel model = new fabucommentModel();
            model.getcomment(userid,editText.getText().toString(),resid,ttlistener);
            Toast.makeText(PictureActivity.this,"发布成功！", Toast.LENGTH_SHORT).show();
        }
    }
}
